import React, { Component, Suspense, lazy } from 'react';
import {Route, Switch, Redirect} from 'react-router-dom'
import {dynamicRouterMap} from './config'
import {getToken} from '@/utils/auth'
import NotFound from '@/pages/notFound' 
import Warp from './warp'

class PrivateRoute extends Component {

    // requireAuth = (component, auth) => {
    //     // const {auth} = this.props
    // }

    render() {
        
        // const DynamicComponent = lazy(()=>import(`@/pages/maintain/index`))
        return (
            <Switch>
            {
                dynamicRouterMap.menus.map((item, index) => {
                const DynamicComponent = lazy (() => import(`@/pages/${item.component}/index`)); 
                return <Route key={index} path={item.path} exact render={props => (
                    <Warp {...props}>
                    <Suspense fallback={<div>loading...</div>}>
                    {
                        !item.auth
                            ? (<DynamicComponent {...props} />)
                            : (getToken()
                                ? (<DynamicComponent {...props} />)
                                : (<Redirect to={{ pathname: "/login", state: {from: props.location}
                                }}/>)
                            )
                    }
                    </Suspense>
                    </Warp>
                )}>
                </Route>
            })
            }
            <Route component={NotFound}/>
        </Switch>
        );
    }
}

export default PrivateRoute;

